package com.android.functionmanagement;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import cn.jpush.android.api.JPushInterface;
import com.android.functionmanagement.globle.ConstantGloble;
import com.android.functionmanagement.service.AlertService;
import com.android.functionmanagement.service.PedometerService;
import com.android.functionmanagement.ui.AlertManometryActivity;
import com.android.functionmanagement.ui.AlertPharmacyActivity;
import com.android.functionmanagement.ui.FamiliarityNumberActivity;
import com.android.functionmanagement.ui.GatherPeriodActivity;
import com.android.functionmanagement.ui.UserInfoActivity;
import com.android.functionmanagement.util.DeviceUtils;
import com.android.functionmanagement.util.LoadUserInfo;
import com.android.functionmanagement.util.SharedPreferencesUtils;
import com.android.functionmanagement.util.UploadData;
import com.uwatch.helowin.R;

/**
 * 首页面
 * 
 * @author fmh
 * 
 */
public class MainActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 普通请求
		// ControlCenter.getInstanse().requestHttp("http://www.weather.com.cn/data/cityinfo/101010100.html",
		// "get", null, this,this);
		setupView();
		//Add by dhy .. {
		startService(new Intent(this, AlertService.class));
		startService(new Intent(this, PedometerService.class));
		// } .. end ..
		//testUpLoadFile();
		//testUpLoadData();
		//testRequest();
		// SharedPreferencesUtils.setParam(this, ConstantGloble.GATHER_DATA_PERIOD, "1");
	}
	
	/**
	 * 请求用户信息test..
	 */
	private void testRequest(){
		HashMap<String, Object> copyFrom = new HashMap<String, Object>();
		copyFrom.put(ConstantGloble.REQHEAD, getreqHeadContent(ConstantGloble.W002));
		copyFrom.put(ConstantGloble.BODY, getCurrentRequestParm());
		JSONObject jsonObject = new JSONObject(copyFrom);
		ControlCenter.getInstanse().requestHttp(ConstantGloble.getBaseUri(this), "post", jsonObject,null,null, this,this);
	}
	
	/**
	 * 上传数据test..
	 */
	private void testUpLoadData(int type ,int pos_type){
		HashMap<String, Object> copyFrom = new HashMap<String, Object>();
		copyFrom.put(ConstantGloble.REQHEAD, getreqHeadContent(ConstantGloble.W003));
		copyFrom.put(ConstantGloble.BODY, new UploadData(this, type, pos_type));
		JSONObject jsonObject = new JSONObject(copyFrom);
		ControlCenter.getInstanse().requestHttp(ConstantGloble.getBaseUri(this), "post", jsonObject,null,null, this,this);
	}
	
	/**
	 * 上传文件test..
	 */
	private void testUpLoadFile() {
//		final HashMap<String, Object> copyFrom = new HashMap<String, Object>();
//		copyFrom.putAll(getreqHeadContent(ConstantGloble.W003));
//		HashMap<String, Object> data = new UploadData(this, 4, 0).getHashMap();
//		copyFrom.putAll(data);
//		ControlCenter.getInstanse().requestHttp(ConstantGloble.getBaseUri(this), "post", null,copyFrom,new File(
//				"/sdcard/charge.mp3"), this,this);

//		final UploadFile uf = new UploadFile();
//		new Thread(new Runnable() {
//
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				String ss = uf.getResult(ConstantGloble.BASE_URL, new File(
//						"/sdcard/charge.mp3"), copyFrom, MainActivity.this);
//				Log.i("DHYCO","ss="+ss);
//			}
//		}).start();
	}
	
	/**
	 * getCurrentRequestParm
	 * @return
	 */
	public HashMap<String, String> getCurrentRequestParm(){
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(ConstantGloble.DEVICEID, DeviceUtils.getIMEI(this));
		return map;
	}
	
	private void setupView() {
		findViewById(R.id.userinfo).setOnClickListener(clickListener);
		findViewById(R.id.familiarity_number).setOnClickListener(clickListener);
		findViewById(R.id.pharmacy_alert).setOnClickListener(clickListener);
		findViewById(R.id.manometry_alert).setOnClickListener(clickListener);
		findViewById(R.id.gather_period).setOnClickListener(clickListener);
		findViewById(R.id.loaduserinfo).setOnClickListener(clickListener);
	}

	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int id = v.getId();
			String userId = SharedPreferencesUtils.getParam(MainActivity.this, ConstantGloble.USERID, "").toString();
			if(R.id.loaduserinfo != id && TextUtils.isEmpty(userId)){
				Toast.makeText(MainActivity.this, "尚未绑定!", Toast.LENGTH_SHORT).show();
				return;
			}
			switch (id) {
			case R.id.userinfo:
				if(!TextUtils.isEmpty(userId)){
					goActivity(UserInfoActivity.class, null, 0, 0);
				}else{
					Toast.makeText(MainActivity.this, "暂无绑定用户", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.familiarity_number:
				goActivity(FamiliarityNumberActivity.class, null, 0, 0);
				break;
			case R.id.pharmacy_alert:
				goActivity(AlertPharmacyActivity.class, null, 0, 0);
				break;
			case R.id.manometry_alert:
				goActivity(AlertManometryActivity.class, null, 0, 0);
				break;
			case R.id.gather_period:
				goActivity(GatherPeriodActivity.class, null, 0, 0);
				break;
			case R.id.loaduserinfo:
				//goActivity(GatherPeriodActivity.class, null, 0, 0);
				LoadUserInfo.getUserInfo(MainActivity.this,true);
				break;

			default:
				break;
			}
		}
	};

	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
	private String getCurrentDate() {
		return df.format(System.currentTimeMillis());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		JPushInterface.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		JPushInterface.onPause(this);
	}
}
